﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class LeaseCancellation
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        internal IList<LeaseCancellationWithdrawalReason> LeaseCancellationWithdrawalReasons
        {
            get
            {
                return InternalLeaseCancellationWithdrawalReasons.Where(leaseCancellationWithdrawalReason => leaseCancellationWithdrawalReason.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<WithdrawalReason> WithdrawalReasons
        {
            get
            {
                return InternalLeaseCancellationWithdrawalReasons.Where(leaseCancellationWithdrawalReason => leaseCancellationWithdrawalReason.IsDeleted == false && leaseCancellationWithdrawalReason.WithdrawalReason.IsDeleted == false).Select(leaseCancellationWithdrawalReason => leaseCancellationWithdrawalReason.WithdrawalReason).ToList();
            }
        }

        #endregion

        #region Add & Remove LeaseCancellationWithdrawalReason

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal LeaseCancellationWithdrawalReason AddLeaseCancellationWithdrawalReason(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var leaseCancellationWithdrawalReason = new LeaseCancellationWithdrawalReason
                                                        {
                                                            RowId = Guid.NewGuid(),
                                                            CreatedDate = DateTime.Now,
                                                        };
            InternalLeaseCancellationWithdrawalReasons.Add(leaseCancellationWithdrawalReason);
            leaseCancellationWithdrawalReason.IsDeleted = false;
            leaseCancellationWithdrawalReason.ModifiedDate = DateTime.Now;
            leaseCancellationWithdrawalReason.ModifiedByUser = modifiedUser;
            return leaseCancellationWithdrawalReason;
        }

        /// <summary>
        /// </summary>
        /// <param name = "leaseCancellationWithdrawalReason"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveLeaseCancellationWithdrawalReason(LeaseCancellationWithdrawalReason leaseCancellationWithdrawalReason, User modifiedUser)
        {
            if (leaseCancellationWithdrawalReason == null)
            {
                throw new ArgumentNullException("leaseCancellationWithdrawalReason");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalLeaseCancellationWithdrawalReasons.Contains(leaseCancellationWithdrawalReason))
            {
                leaseCancellationWithdrawalReason.Delete(modifiedUser);
            }
        }

        #endregion

        #region Set WithdrawalReasons

        /// <summary>
        /// </summary>
        /// <param name = "withdrawalReasons"></param>
        /// <param name = "modifiedUser"></param>
        public void SetWithdrawalReasons(IList<WithdrawalReason> withdrawalReasons, User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (withdrawalReasons != null)
            {
                var leaseCancellationWithdrawalReasons = InternalLeaseCancellationWithdrawalReasons.ToList();
                leaseCancellationWithdrawalReasons.ForEach(leaseCancellationWithdrawalReason => leaseCancellationWithdrawalReason.Delete(modifiedUser));
                foreach (var withdrawalReason in withdrawalReasons.Distinct())
                {
                    var currentLeaseCancellationWithdrawalReason = leaseCancellationWithdrawalReasons.FirstOrDefault(leaseCancellationWithdrawalReason => leaseCancellationWithdrawalReason.WithdrawalReason == withdrawalReason);
                    if (currentLeaseCancellationWithdrawalReason != null)
                    {
                        currentLeaseCancellationWithdrawalReason.CancelDelete(modifiedUser);
                    }
                    else
                    {
                        currentLeaseCancellationWithdrawalReason = AddLeaseCancellationWithdrawalReason(modifiedUser);
                        currentLeaseCancellationWithdrawalReason.WithdrawalReason = withdrawalReason;
                    }
                }
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (DeductibleAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseCancellation", "DeductibleAmount"));
            }
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<LeaseCancellationWithdrawalReason>) LeaseCancellationWithdrawalReasons).ForEach(leaseCancellationWithdrawalReason => leaseCancellationWithdrawalReason.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}